 export class Request {
    options = {
        baseUrl: 'http://127.0.0.1/',
        dataType: 'json',
        timeout: 5000,
        loading: true,
        loadingText: '加载中',
        loadingMask: true,
        responseType: 'text',
        header: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
    };
    isShow = false;
    constructor(options = {}) {
        this.options = Object.assign(this.options, options)
    }
    // 请求拦截
    static requestBefore = (config) => config;
    // 响应拦截
    static requestAfter = (response) => response;
    // 判断是否是完整uri
    static isCompleteURL = (url) => /(http|https):\/\/([\w.]+\/?)\S*/.test(url);

    // 拦截器
    interceptors = {
        // 请求拦截器
        request: (func) => {
            if (func) {
                HttpRequest.requestBefore = func
            }
            else {
                HttpRequest.requestBefore = (request) => request
            }
        },
        // 响应拦截器
        response: (func) => {
            if (func) {
                HttpRequest.requestAfter = func
            }
            else {
                HttpRequest.requestAfter = (response) => response
            }
        }
    }

    showLoading(options, func) {
        if (func && typeof func == 'function') {
            func()
        } else {
            uni.showLoading({
                title: options.loadingText,
                mask: options.loadingMask,
            })
        }
    }

    hideLoading(func) {
        if (func && typeof func == 'function') {
            func()
        } else {
            uni.hideLoading()
        }
    }

    /**
     * 发起网络请求
     * @param options 请求参数
     * @returns Promise
     */
    request(options = {}) {
        options.url = HttpRequest.isCompleteURL(options.url) ? options.url : this.options.baseUrl + options.url;
        // 合并请求参数重复则覆盖
        options = Object.assign(this.options, HttpRequest.requestBefore(options));
        if (!this.isShow && options.loading) {
            this.isShow = true;
            this.showLoading(options)
        }
        return new Promise((resolve, reject) => {
            options.success = (res) => {
                resolve(HttpRequest.requestAfter(res))
            }
            options.fail = (err) => {
                reject(HttpRequest.requestAfter(err))
            }
            options.complete = () => {
                if (this.isShow && this.options.loading) {
                    this.hideLoading()
                    this.isShow = false;
                }
            }
            let http = wx || uni;
            http.request(options)
        })
    }

    get(url, data = '', options = {}) {
        options.url = url
        options.data = data
        options.method = 'GET'
        return this.request(options)
    }

    post(url, data = {}, options = {}) {
        options.url = url
        options.data = data
        options.method = 'POST'
        return this.request(options)
    }
 }
const request = new Request({})
request.interceptors.request = (config) => {
    return config;
}

request.interceptors.response = (config) => {
    return config;
}
request.request({
    url: '/api',
    data: data
})